<?php

namespace App\Models\Game\Config\Game;

use App\Models\Game\Model;
use App\Models\Game\Config\Game;
use App\Models\Traits\Game\LogsActivity;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class System extends Model
{
    use LogsActivity;

    /**
     * 数据表名称
     *
     * @var string
     */
//    protected $table = 't_config_game_sys_copy1';
    protected $table = 't_config_game_sys';

    /**
     * 不可批量赋值的属性
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * 进行类型转换的字段
     *
     * @var array
     */
    protected $casts = [
        'config' => 'array',
        'created' => 'timestamp',
        'updated' => 'timestamp'
    ];

    /**
     * 获得游戏信息
     *
     * @return BelongsTo
     */
    public function game(): BelongsTo
    {
        return $this->belongsTo(Game::class, 'game_id', 'code');
    }

    /**
     * 查询一条数据
     *
     * @param string $gameCode
     * @return \Illuminate\Database\Eloquent\Model
     */
    protected function findByGame($gameCode)
    {
        return $this->newQuery()
            ->where('game_id', $gameCode)
            ->first();
    }

    /**
     * 更新或创建一条数据
     *
     * @param string $gameCode
     * @param array $config
     * @return \Illuminate\Database\Eloquent\Model
     */
    protected function updateOrCreateByGame($gameCode, array $config = [])
    {
        return $this->newQuery()->updateOrCreate([
            'game_id' => $gameCode
        ], [
            'config' => $config,
        ]);
    }
}
